package com.itheima.behavior.service.impl;

import com.itheima.behavior.service.LikeService;
import com.itheima.common.redis.CacheService;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.utils.thread.MpThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private CacheService cacheService;

    private static final String USER_LIKE_KEY = "user_like:%s";
    private static final String LIKE_COUNT_KEY = "like_count";

    @Override
    public ResponseResult likeBook(Integer id) {
        return handleLike("book", id.toString());
    }

    @Override
    public ResponseResult cancelLikeBook(Integer id) {
        return handleCancelLike("book", id.toString());
    }

    @Override
    public ResponseResult likeNote(String id) {
        return handleLike("note", id);
    }

    @Override
    public ResponseResult cancelLikeNote(String id) {
        return handleCancelLike("note", id);
    }

    @Override
    public ResponseResult likeReview(String id) {
        return handleLike("review", id);
    }

    @Override
    public ResponseResult cancelLikeReview(String id) {
        return handleCancelLike("review", id);
    }

    /**
     * 统一处理点赞
     */
    private ResponseResult handleLike(String type, String targetId) {
        like(type, targetId);
        Map<String, Object> result = new HashMap<>();
        result.put("liked", true);
        result.put("count", getLikeCount(type, targetId));
        return ResponseResult.okResult(result);
    }

    /**
     * 统一处理取消点赞
     */
    private ResponseResult handleCancelLike(String type, String targetId) {
        cancelLike(type, targetId);
        Map<String, Object> result = new HashMap<>();
        result.put("liked", false);
        result.put("count", getLikeCount(type, targetId));
        return ResponseResult.okResult(result);
    }

    /**
     * 点赞核心方法
     */
    private void like(String type, String targetId) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        String userKey = String.format(USER_LIKE_KEY, userId);
        String targetKey = type + ":" + targetId;

        cacheService.hPut(userKey, targetKey, "1");
        cacheService.hIncrBy(LIKE_COUNT_KEY, targetKey, 1);
    }

    /**
     * 取消点赞核心方法
     */
    private void cancelLike(String type, String targetId) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        String userKey = String.format(USER_LIKE_KEY, userId);
        String targetKey = type + ":" + targetId;

        cacheService.hDelete(userKey, targetKey);
        cacheService.hIncrBy(LIKE_COUNT_KEY, targetKey, -1);
    }

    /**
     * 获取点赞数
     */
    private Long getLikeCount(String type, String targetId) {
        String targetKey = type + ":" + targetId;
        Object count = cacheService.hGet(LIKE_COUNT_KEY, targetKey);
        return count != null ? Long.parseLong(count.toString()) : 0L;
    }

    /**
     * 是否点赞过
     */
    public boolean isLiked(String type, String targetId) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        String userKey = String.format(USER_LIKE_KEY, userId);
        String targetKey = type + ":" + targetId;
        return cacheService.hExists(userKey, targetKey);
    }
}